package com.fnzx.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.aliyun.oss.OSS;
import com.fnzx.entity.IndexLogoUrlEntity;
import com.fnzx.service.IndexLogoUrlService;
import com.fnzx.util.PageUtils;
import com.fnzx.util.R;
import com.fnzx.util.oss.OssUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;


/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2021-11-12 14:02:32
 */
@Api(tags = "企业logo")
@RestController
@RequestMapping("iot/indexlogourl")
public class IndexLogoUrlController {
    @Autowired
    private IndexLogoUrlService indexLogoUrlService;

    /**
     * 列表
     */
    @ApiOperation(value = "分页查询",notes = "分页查询logo(page->当前页面,limit->每页记录数)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "当前页",paramType = "query",required = true,dataType = "String"),
            @ApiImplicitParam(name = "limit",value = "每页记录数",paramType = "query",required = true,dataType = "String")
    })
    @GetMapping("/list")
    public R list(@ApiIgnore @RequestParam Map<String, Object> params){
        PageUtils page = indexLogoUrlService.queryPage(params);

        return R.ok().put("page", page);
    }

    @ApiOperation(value = "查询出所有logo",notes = "查询出所有logo")
    @GetMapping("/list/all")
    public R listAll(){
        List<IndexLogoUrlEntity> list = indexLogoUrlService.list();
        return R.ok().put("list", list);
    }


    /**
     * 信息
     */
    @ApiOperation(value = "根据id查询",notes = "根据id查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "id",paramType = "query",required = true,dataType = "String")
    })
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") String id){
		IndexLogoUrlEntity indexLogoUrl = indexLogoUrlService.getById(id);

        return R.ok().put("indexLogoUrl", indexLogoUrl);
    }

    /**
     * 保存
     */
    @ApiOperation(value = "添加logo",notes = "添加logo")
    @PostMapping("/save")
    public R save(@RequestBody IndexLogoUrlEntity indexLogoUrl){
		indexLogoUrlService.save(indexLogoUrl);

        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation(value = "修改logo",notes = "修改logo")
    @PostMapping("/update")
    public R update(@RequestBody IndexLogoUrlEntity indexLogoUrl){
        String logoUrl = indexLogoUrlService.getById(indexLogoUrl.getId()).getLogoUrl();
        if (!logoUrl.equals(indexLogoUrl)) {
            OssUtils.delete(logoUrl);
        }
        indexLogoUrlService.updateById(indexLogoUrl);
        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除logo",notes = "删除logo")
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] ids){
        for (Long id : ids) {
            String logoUrl = indexLogoUrlService.getById(id).getLogoUrl();
            OssUtils.delete(logoUrl);
        }
		indexLogoUrlService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
